﻿/*
-----------------------------------------------------------------------------
This source file is part of Cell Cloud.

Copyright (c) 2009-2012 Cell Cloud Team (cellcloudproject@gmail.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/

#ifndef CellQueue_h
#define CellQueue_h

#include "CellPrerequisites.h"

namespace cell
{
/** 线程安全的模板队列。
@author
	Jiangwei Xu
*/
template<typename T>
class Queue : public GeneralAlloc
{
public:
	/**
	*/
	Queue(void);

	/**
	*/
	Queue(size_t capacity);

	/**
	*/
	virtual ~Queue(void);

public:
	/** 队尾插入元素。
	*/
	bool offer(T element);

	/** 移除队首元素。
	*/
	bool poll(T& element);

	/**
	*/
	bool peek(T& element);

	/** 判断是否为满。
	*/
	bool full(void);

	/** 判断是否为空。
	*/
	bool empty(void);

	/** 返回队列大小。
	*/
	size_t size(void);

	/** 返回队列容量。
	*/
	size_t capacity(void);

private:
	std::vector<T> mQueue;
	size_t mCapacity;
	CELL_AUTO_MUTEX;
};

//
// Implemention
//

//-----------------------------------------------------------------------
template<typename T>
Queue<T>::Queue(void)
	: mCapacity(8)
{
}
//-----------------------------------------------------------------------
template<typename T>
Queue<T>::Queue(size_t capacity)
	: mCapacity(capacity)
{
}
//-----------------------------------------------------------------------
template<typename Type>
Queue<Type>::~Queue(void)
{
}
//-----------------------------------------------------------------------
template<typename T>
bool Queue<T>::offer(T element)
{
	CELL_LOCK_AUTO_MUTEX;

	mQueue.push_back(element);

	if (mQueue.size() > mCapacity)
	{
		mCapacity = mQueue.size();
	}

	return true;
}
//-----------------------------------------------------------------------
template<typename T>
bool Queue<T>::poll(T& element)
{
	CELL_LOCK_AUTO_MUTEX;

	if (mQueue.empty())
		return false;

	std::vector<T>::iterator iter = mQueue.begin();
	element = (*iter);
	mQueue.erase(iter);
	return true;
}
//-----------------------------------------------------------------------
template<typename T>
bool Queue<T>::peek(T& element)
{
	CELL_LOCK_AUTO_MUTEX;

	if (mQueue.empty())
		return false;

	element = (*mQueue.begin());
	return true;
}
//-----------------------------------------------------------------------
template<typename T>
bool Queue<T>::full(void)
{
	CELL_LOCK_AUTO_MUTEX;
	return (mCapacity == mQueue.size());
}
//-----------------------------------------------------------------------
template<typename T>
bool Queue<T>::empty(void)
{
	CELL_LOCK_AUTO_MUTEX;
	return (mQueue.empty());
}
//-----------------------------------------------------------------------
template<typename T>
size_t Queue<T>::capacity(void)
{
	CELL_LOCK_AUTO_MUTEX;
	return mCapacity;
}
//-----------------------------------------------------------------------
template<typename T>
size_t Queue<T>::size(void)
{
	CELL_LOCK_AUTO_MUTEX;
	return mQueue.size();
}

} // end namespace cell

#endif // CellQueue_h
